import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//自定义数据库连接池
public class MyDataSource {

    private static Map<String, String> cache = new HashMap<>();  //缓存配置信息

    public MyDataSource(String user, String pwd, String driver, String url) {
        cache.put("user", user);
        cache.put("pwd", pwd);
        cache.put("driver", driver);
        cache.put("url", url);
    }

    public MyDataSource(String name, String value){
        cache.put(name, value);
    }

    public Connection getConn() throws SQLException {
        BasicDataSource ds = new BasicDataSource();
        ds.setUsername(cache.get("user"));
        ds.setPassword(cache.get("pwd"));
        ds.setDriverClassName(cache.get("driver"));
        ds.setUrl(cache.get("url"));
        return ds.getConnection();
    }

}
